﻿#include <iostream>
#include <string>
#include "Dictionary.h"

using namespace std;

Dictionary::Dictionary()
{
	for(int i = 0; i < 26; i++)
	{
		ht[i] = NULL;
	}
	m=97;
	
}

void Dictionary::Show()
{
	char word;
	cout << "查询首字母开头单词：";
	cin >> word;
	if ((word >= 'a' && word <= 'z') || (word >= 'A' && word <= 'Z'))
	{
		if (word >= 'A' && word <= 'Z')
		{
			word = word + 32;
		}

		Init_if(word);

		while (!iutfile.eof())
		{
			English te;
			iutfile >> te.english;
			iutfile >> te.chinese;
			if (!iutfile.eof())
			{
				cout << te.english << " : " << te.chinese << endl;
			}
		}

		iutfile.clear();
		iutfile.close();
	}
}

void Dictionary::Lookfor(string s)
{
	int ade = Delete_lookfor(s);
	
	if(ade != 0)
	{
		int temp = (int)s[1] % m;
		English *p = ht[temp];
			
		while(p != NULL)
		{
			if(p->english == s)
			{	
				cout << p->english << " : " << p->chinese << endl;
				system("pause");
				Clean();
				break;
			}
			else
			{
				p = p->next;
			}
		} 
	}
	else
	{
		cout << "该单词不存在是否添加Y or N：";

		char ch;
		cin >> ch;

		if(ch == 'y' || ch == 'Y')
		{
			Clean();
			add(s);
		} 
	}
}



void Dictionary::Delete()
{
	string st;
	int cnt = 1;
	int adre;

	cout << "输入你要删除的单词："; 
	cin >>  st;
	adre = Delete_lookfor(st);

	int num = (int)st[1] % m;

	English *p = ht[num];
	English *q = NULL;
	
	if(adre == 0)
	{
		cout << "没有该单词" <<endl;
		Clean();
		system("pause");
	}
	else
	{
		while(p != NULL && cnt < adre-1)
		{
			p = p->next;
			cnt++;
		}
		
		if(adre != 1)
		{
			q = p->next;
			p->next = q->next;
			delete q;
		}
		else
		{
			ht[num] = p->next;
			delete p;
		}

		CleanS();
		Init_of(st[0]);

		for(int i = 0; i < 26; i++)
		{
			English *pt = ht[i];
			
			while(pt)
			{
				Se.push(pt->english);
				Sc.push(pt->chinese);
				pt = pt->next;
			}
			
			while(!Se.empty() && !Sc.empty())
			{
				outfile << Se.top() << " " << Sc.top();
				Se.pop();
				Sc.pop();
				
				if(!Se.empty() && !Sc.empty())
				{
					outfile << endl;
				}
			}
		}

		cout << "删除成功!" << endl; 
		system("pause");

		outfile.clear();
		outfile.close();
		Clean();
	}
}

int Dictionary::Delete_lookfor(string s)   
{
	Init_if(s[0]);
	file();
	int i = (int)s[1] % m;
	English *p = ht[i];
	
	while(p)
	{
		Q.push(p->english);
		p = p->next;
	}
	
	int num = Q.size();

	string* a = new string[num];
	
	for(int j = 0; j < num; j++)
	{
		a[j] = Q.front();
		Q.pop(); 
	}
	
	//二分法查找
	int low = 0;
	int high = num-1;
	int mid;

	while(low <= high)
	{
		mid = (low + high) / 2;
		
		if(s > a[mid])
		{
			high = mid - 1;
		}
		else if(s < a[mid])
		{
			low = mid + 1;
		}
		else
		{
			return mid + 1;      //查找成功，返回位置; 
		}
	}
	
	return 0;					//返回0表示没有找到 
}

void Dictionary::add(string str)
{
	int isp;               //标记判断是否已经插入 
	string add_chinese;

	int cnt;               //用来接收lookfor函数的返回值 
		
	str = change(str);     //将所有大写字母转为小写字母
		
	cnt = Delete_lookfor(str);
	
	if(cnt != 0)
	{
		cout << "该单词已存在" << endl;
		system("pause");
	}
	else
	{
		cout << "输入对应的汉语和词性： ";
		cin >> add_chinese;

		int i = (int)str[1] % m;
		English *p = ht[i];
		
		if(ht[i] == NULL)
		{
			isp = 1;
			English *s = new English;
			s->english = str;
			s->chinese = add_chinese;
			s->next = ht[i];
			ht[i] = s;
		}
		else if(p->english < str)
		{
			isp = 1;
			English *s = new English;
			s->english = str;
			s->chinese = add_chinese;
			s->next = p;
			ht[i] = s;
		}
		else
		{	
			English *pt = p->next;

			while(pt)
			{
				if(pt->english < str)
				{
					isp = 1;
					English *s = new English;
					s->english = str;
					s->chinese = add_chinese;
					s->next = pt;
					p->next = s;
					break;
				}
				else
				{
					p = p->next;
					pt = pt->next;
				}
			}

		}
		
		if(isp != 1)
		{
			English *s = new English;
			s->english = str;
			s->chinese = add_chinese;
			p->next = s;
		}
		else
		{
			isp = 0;
		}
		
		CleanS();
		
		Init_of(str[0]);

		for(int i = 0; i < 26; i++)
		{
			English *pt = ht[i];
			
			while(pt)
			{
				Se.push(pt->english);
				Sc.push(pt->chinese);
				pt = pt->next;
			}
			while(!Se.empty() && !Sc.empty())
			{
				outfile << Se.top() << " " << Sc.top();
				Se.pop();
				Sc.pop();
				
				outfile << endl;
			}
		}

		outfile.clear();
		outfile.close();
		Clean();

		cout << "添加成功"; 
		system("pause");
	}
	
	Clean();
}

string Dictionary::change(string s)
{
	for(int i = 0; s[i] != '\0'; i++)
	{
		if(s[i] >=65 && s[i] <= 90)
		{
			s[i] = s[i] + 32;
		}
	}
	
	return s;
}

void Dictionary::CleanS()
{
	while (!Se.empty())
	{
		Se.pop();
	}

	while (!Sc.empty())
	{
		Sc.pop();
	}
}

void Dictionary::Clean()
{

	for (int i = 0; i < 26; i++)
	{
		ht[i] = NULL;
	}
}

void Dictionary::file()               //用的时候表示已经打开一个文件了，把对应首字母的单词存入数组   建立开散列表的时候应从第二个字母开始 
{
	int isp = 0;
	int num;
	while (!iutfile.eof())	          //eof文件尾
	{
		isp = 1;
		English *s = new English;
		iutfile >> s->english;
		iutfile >> s->chinese;
		int cnt = (int)s->english[1];
		num = cnt % m;	              //m是a的asc 假如cnt是a num即为0 就存储在[0]
		s->next = ht[num];
		ht[num] = s;
	}

	if (isp == 0)
	{
		cout << "文件为空" << endl;
	}

	iutfile.clear();
	iutfile.close();
}  

void Dictionary::Init_if(char n)
{
	int num;
	
	switch(n)
	{
		case 'a':
			iutfile.open("C:\\Users\\ALIENWARE\\Desktop\\QT\\word\\a.txt");
			break;
			
		case 'b':
			iutfile.open("C:\\Users\\ALIENWARE\\Desktop\\QT\\word\\b.txt");
			break;
		
		case 'c':
			iutfile.open("C:\\Users\\ALIENWARE\\Desktop\\QT\\word\\c.txt");
			break;
			
		case 'd':
			iutfile.open("C:\\Users\\ALIENWARE\\Desktop\\QT\\word\\d.txt");
			break;
			
		case 'e':
			iutfile.open("C:\\Users\\ALIENWARE\\Desktop\\QT\\word\\e.txt");
			break;
			
		case 'f':
			iutfile.open("C:\\Users\\ALIENWARE\\Desktop\\QT\\word\\f.txt");
			break;
			
		case 'g':
			iutfile.open("C:\\Users\\ALIENWARE\\Desktop\\QT\\word\\g.txt");
			break;
			
		case 'h':
			iutfile.open("C:\\Users\\ALIENWARE\\Desktop\\QT\\word\\h.txt");
			break;
		
		case 'i':
			iutfile.open("C:\\Users\\ALIENWARE\\Desktop\\QT\\word\\i.txt");
			break;
		
		case 'j':
			iutfile.open("C:\\Users\\ALIENWARE\\Desktop\\QT\\word\\j.txt");
			break;
			
		case 'k':
			iutfile.open("C:\\Users\\ALIENWARE\\Desktop\\QT\\word\\k.txt");
			break;
		
		case 'l':
			iutfile.open("C:\\Users\\ALIENWARE\\Desktop\\QT\\word\\l.txt");
			break;
			
		case 'm':
			iutfile.open("C:\\Users\\ALIENWARE\\Desktop\\QT\\word\\m.txt");
			break;
		
		case 'n':
			iutfile.open("C:\\Users\\ALIENWARE\\Desktop\\QT\\word\\n.txt");
			break;
			
		case 'o':
			iutfile.open("C:\\Users\\ALIENWARE\\Desktop\\QT\\word\\o.txt");
			break;
			
		case 'p':
			iutfile.open("C:\\Users\\ALIENWARE\\Desktop\\QT\\word\\p.txt");
			break;
			
		case 'q':
			iutfile.open("C:\\Users\\ALIENWARE\\Desktop\\QT\\word\\q.txt");
			break;
			
		case 'r':
			iutfile.open("C:\\Users\\ALIENWARE\\Desktop\\QT\\word\\r.txt");
			break;
			
		case 's':
			iutfile.open("C:\\Users\\ALIENWARE\\Desktop\\QT\\word\\s.txt");
			break;
			
		case 't':
			iutfile.open("C:\\Users\\ALIENWARE\\Desktop\\QT\\word\\t.txt");
			break;
			
		case 'u':
			iutfile.open("C:\\Users\\ALIENWARE\\Desktop\\QT\\word\\u.txt");
			break;
			
		case 'v':
			iutfile.open("C:\\Users\\ALIENWARE\\Desktop\\QT\\word\\v.txt");
			break;
			
		case 'w':
			iutfile.open("C:\\Users\\ALIENWARE\\Desktop\\QT\\word\\w.txt");
			break;
			
		case 'x':
			iutfile.open("C:\\Users\\ALIENWARE\\Desktop\\QT\\word\\x.txt");
			break;
			
		case 'y':
			iutfile.open("C:\\Users\\ALIENWARE\\Desktop\\QT\\word\\y.txt");
			break;
			
		case 'z':
			iutfile.open("C:\\Users\\ALIENWARE\\Desktop\\QT\\word\\z.txt");
			break;	
	}
}

void Dictionary::Init_of(char n)
{
	int num;
	
	switch(n)
	{
		case 'a':
			outfile.open("C:\\Users\\ALIENWARE\\Desktop\\QT\\word\\a.txt");
			break;
			
		case 'b':
			outfile.open("C:\\Users\\ALIENWARE\\Desktop\\QT\\word\\b.txt");
			break;
		
		case 'c':
			outfile.open("C:\\Users\\ALIENWARE\\Desktop\\QT\\word\\c.txt");
			break;
			
		case 'd':
			outfile.open("C:\\Users\\ALIENWARE\\Desktop\\QT\\word\\d.txt");
			break;
			
		case 'e':
			outfile.open("C:\\Users\\ALIENWARE\\Desktop\\QT\\word\\e.txt");
			break;
			
		case 'f':
			outfile.open("C:\\Users\\ALIENWARE\\Desktop\\QT\\word\\f.txt");
			break;
			
		case 'g':
			iutfile.open("C:\\Users\\ALIENWARE\\Desktop\\QT\\word\\g.txt");
			break;
			
		case 'h':
			iutfile.open("C:\\Users\\ALIENWARE\\Desktop\\QT\\word\\h.txt");
			break;
		
		case 'i':
			iutfile.open("C:\\Users\\ALIENWARE\\Desktop\\QT\\word\\i.txt");
			break;
		
		case 'j':
			iutfile.open("C:\\Users\\ALIENWARE\\Desktop\\QT\\word\\j.txt");
			break;
			
		case 'k':
			iutfile.open("C:\\Users\\ALIENWARE\\Desktop\\QT\\word\\k.txt");
			break;
		
		case 'l':
			iutfile.open("C:\\Users\\ALIENWARE\\Desktop\\QT\\word\\l.txt");
			break;
			
		case 'm':
			iutfile.open("C:\\Users\\ALIENWARE\\Desktop\\QT\\word\\m.txt");
			break;
		
		case 'n':
			iutfile.open("C:\\Users\\ALIENWARE\\Desktop\\QT\\word\\n.txt");
			break;
			
		case 'o':
			iutfile.open("C:\\Users\\ALIENWARE\\Desktop\\QT\\word\\o.txt");
			break;
			
		case 'p':
			iutfile.open("C:\\Users\\ALIENWARE\\Desktop\\QT\\word\\p.txt");
			break;
			
		case 'q':
			iutfile.open("C:\\Users\\ALIENWARE\\Desktop\\QT\\word\\q.txt");
			break;
			
		case 'r':
			iutfile.open("C:\\Users\\ALIENWARE\\Desktop\\QT\\word\\r.txt");
			break;
			
		case 's':
			iutfile.open("C:\\Users\\ALIENWARE\\Desktop\\QT\\word\\s.txt");
			break;
			
		case 't':
			iutfile.open("C:\\Users\\ALIENWARE\\Desktop\\QT\\word\\t.txt");
			break;
			
		case 'u':
			iutfile.open("C:\\Users\\ALIENWARE\\Desktop\\QT\\word\\u.txt");
			break;
			
		case 'v':
			iutfile.open("C:\\Users\\ALIENWARE\\Desktop\\QT\\word\\v.txt");
			break;
			
		case 'w':
			iutfile.open("C:\\Users\\ALIENWARE\\Desktop\\QT\\word\\w.txt");
			break;
			
		case 'x':
			iutfile.open("C:\\Users\\ALIENWARE\\Desktop\\QT\\word\\x.txt");
			break;
			
		case 'y':
			iutfile.open("C:\\Users\\ALIENWARE\\Desktop\\QT\\word\\y.txt");
			break;
			
		case 'z':
			iutfile.open("C:\\Users\\ALIENWARE\\Desktop\\QT\\word\\z.txt");
			break;	
	}
}
